package paginator.classes
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Menu;
	
	import paginator.buttons.LastBtn;
	import paginator.buttons.NextBtn;
	import paginator.helpers.MenuButtonsCreator;
	import paginator.paginateEvents.PagesValueChange;
	
	/**
	 * This class support all buttons
	 * Next, Jump forrward and Jump to last page
	 *  
	 * @author Hellix
	 */	
	public class NextButtonsClass extends ButtonsClass implements IObserver
	{
		//---------------------------------------------------------------
		// <------ PRIVATE PROPERTIES ------>
		//---------------------------------------------------------------
		//{
		/**
		 *Button object of next button 
		 */		
		private var _nextBtn:NextBtn;
		/**
		 *Button object of jump to last page button 
		 */		
		private var _lastBtn:LastBtn;
		
		//}
		//---------------------------------------------------------------
		// <------ PUBLIC METHODS ------>
		//---------------------------------------------------------------
		//{

		/**
		 * Ctor 
		 * @param NextBtn nextBtn NextBtn object
		 * @param LastBtn lastBtn LastBtn object 
		 */		
		public function NextButtonsClass(nextBtn:NextBtn, lastBtn:LastBtn)
		{
			super();
			
			_nextBtn = nextBtn;
			_lastBtn = lastBtn;
			
			_nextBtn.heightNextBtn = super._arrowBtnHeight;
			_lastBtn.height = super._arrowBtnHeight;
		}
		

		/**
		 * This function notify paginator manager 
		 * */
		public function notifyObserver():void
		{
			_observer.updateElements();
		}
		
		/**
		 * This function sets paginator manager and register required events
		 *  
		 * @param PaginatorManager mainManager Object PaginatorManager
		 */		
		override public function setObserver(mainManager:PaginatorManager):void
		{
			if (_nextBtn.isMenuRefresh)
			{
				super._isMenuRefresh = true;
				_nextBtn.isMenuRefresh = false;
			}
			
			super.setObserver(mainManager);
			
			super._observer.paginator.addEventListener(NextBtn.CLICK_SKIPNEXT, onContextMenuButton);
			super._observer.paginator.ID_NextButtons.ID_Next.addEventListener(MouseEvent.CLICK, onClickNext);
			super._observer.addEventListener(PagesValueChange.NUMBEROFPAGES_CHANGE, onNumOfPagesChange);
			super._observer.paginator.ID_First.addEventListener(MouseEvent.CLICK, onClickFirst);
		}
		
		//}
		//---------------------------------------------------------------
		// <------ PROTECTED METHODS ------>
		//---------------------------------------------------------------
		//{
		
		override protected function onContextMenuButton(event:Event):void
		{
			super.onContextMenuButton(event);
			
			if (super._menuButton)
			{
				super._xPositionMenu = _observer.paginator.x + _nextBtn.x 
					+ _nextBtn.ID_Next.width;
				super._yPositionMenu = _observer.paginator.y + _nextBtn.ID_SkipNext.height;
				super._menuButton.show(super._xPositionMenu, super._yPositionMenu);
			}
		}
		//}
		//---------------------------------------------------------------
		// <------ PRIVATE METHODS ------>
		//---------------------------------------------------------------
		//{

		private function onClickNext(event:MouseEvent):void
		{
			if (_observer.pages >= _observer.selectedPage)
			{
				_observer.selectedPage += 1;
				//_observer.updateElements();
			}
		}

		private function onNumOfPagesChange(event:PagesValueChange):void
		{
			if (event.numberOfPages < 50)
			{
				_nextBtn.visibleSkipNextButton = false;
				super._menuButton = null;
			}
		}
		
		private function onClickFirst(event:MouseEvent):void
		{
			super.jumpFirstOrLastPage(0);
		}
		
		//}
	}
}